﻿using WebSoftSeo.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSoftSeo.Models;

namespace WebSoftSeo.Areas.Admin.Controllers
{
    public class ContactController : Controller
    {
        private WebSoftSeoEntities db = new WebSoftSeoEntities();

        //
        // GET: /Admin/Contact/

        public ActionResult Index()
        {
            int pageNo = 0;
            foreach (var key in Request.Form.AllKeys)
            {
                if (key.StartsWith("PageNo:"))
                {
                    pageNo = int.Parse(key.Substring(7));
                    break;
                }
            }

            PagerInfo pi = PagerInfo.Get("Contact", 10);
            pi.RowCount = db.Contacts.OrderByDescending(p => p.ID).Count();
            pi.Navigate(pageNo);
            int StartRow = pi.PageNo * pi.PageSize;
            var contact = db.Contacts.OrderByDescending(a => a.ID).Skip(StartRow).Take(pi.PageSize);
            return View(contact);
        }
        [HttpPost, ActionName("Index")]
        public ActionResult Index1()
        {
            int pageNo = 0;
            foreach (var key in Request.Form.AllKeys)
            {
                if (key.StartsWith("PageNo:"))
                {
                    pageNo = int.Parse(key.Substring(7));
                    break;
                }
            }
            PagerInfo pi = PagerInfo.Get("Contact", 10);
            pi.RowCount = db.Contacts.OrderByDescending(p => p.ID).Count();
            pi.Navigate(pageNo);
            int StartRow = pi.PageNo * pi.PageSize;
            var contact = db.Contacts.OrderByDescending(a => a.ID).Skip(StartRow).Take(pi.PageSize);
            return PartialView("IndexAjax", contact);
        }
        //
        // GET: /Admin/Contact/Delete

        public ActionResult Delete(int id = 0)
        {
            Contact contact = db.Contacts.Find(id);
            if (contact == null)
            {
                return HttpNotFound();
            }
            ViewBag.ContactId = new SelectList(db.Contacts, "ID", "Name");
            return View(contact);
        }

        //
        // POST: /Admin/Contact/Delete

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Contact contact = db.Contacts.Find(id);
            db.Contacts.Remove(contact);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
